/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<String> ret;
    public List<String> binaryTreePaths(TreeNode root) {
        ret = new ArrayList<>();
        binaryTreePaths(new StringBuilder(), root);
        return ret;
    }
    public void binaryTreePaths(StringBuilder _path, TreeNode root) {
        StringBuilder path = new StringBuilder(_path);
        path.append(Integer.toString(root.val));

        if (root.left == null && root.right == null) {
            ret.add(path.toString());
            return;
        }
        path.append("->");
        if (root.left != null) binaryTreePaths(path, root.left);
        if (root.right != null) binaryTreePaths(path, root.right);

    }
}